//
//  HomeXieYView.m
//  NtoccDriverNew
//
//  Created by 张天兴 on 2023/6/6.
//  Copyright © 2023 GCY. All rights reserved.
//

#import "HomeXieYView.h"


@implementation HomeXieYView

- (instancetype)initWithFrame:(CGRect)frame Url:(NSString *)url{
    
    self = [super initWithFrame:frame];
    if (self) {
//        self._urlString = @"http://36.137.133.24:8000/driver.html";
        self._urlString = url;

        self.isUrlloaded = NO;
        [self setUpUI];
    }
    return self;
}
- (void)setUpUI{
    [self addSubview:self.bgView];
    [self.bgView addSubview:self.contentView];
    self.webView.backgroundColor = [UIColor whiteColor];
    [self.contentView addSubview:self.comitButton];

    if (@available(iOS 11.0, *)) {
        self.webView.scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
//    [self.webView addSubview:self.fontSlider];
    
//    [self.contentView addSubview:self.progressLine];
}
- (UIView *)bgView{
    if(!_bgView){
        self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        self.bgView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    }
    return _bgView;
}
- (UIView *)contentView{
    if(!_contentView){
        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(40, 150, SCREEN_WIDTH - 80, SCREEN_HEIGHT - 300)];
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.contentView.layer.masksToBounds = YES;
        self.contentView.layer.cornerRadius = 10;
    }
    return _contentView;
}
- (UIButton *)comitButton{
    if(!_comitButton){
        self.comitButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.comitButton setTitle:@"已知晓" forState:UIControlStateNormal];
        [self.comitButton setBackgroundColor:[UIColor colorWithRed:233/255.0 green:234/255.0 blue:236/255.0 alpha:1]];
        self.comitButton.layer.masksToBounds = YES;
        self.comitButton.layer.cornerRadius = 5;
        self.comitButton.tag = 301;
        [self.comitButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.comitButton.userInteractionEnabled = NO;
        self.comitButton.frame = CGRectMake(0,SCREEN_HEIGHT - 300 -45 , SCREEN_WIDTH - 80, 45);
    }
    return _comitButton;
}

#pragma mark - WKNavigationDelegate
- (void)webView:(WKWebView *)webView didStartProvisionalNavigation:(WKNavigation *)navigation{
    NSLog(@"开始加载");

}
// 页面加载完成之后调用
- (void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
    NSLog(@"加载完成");
    self.isUrlloaded = YES;
    
    //禁止用户选择
    //    [webView evaluateJavaScript:@"document.documentElement.style.webkitUserSelect='none';" completionHandler:nil];
    //    [webView evaluateJavaScript:@"document.activeElement.blur();" completionHandler:nil];
}
// 页面加载失败时调用
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(WKNavigation *)navigation{
    NSLog(@"加载失败");
}

- (WKWebView *)webView{
    if (!_webView) {
        //适配文字大小
        NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
        WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
        WKUserContentController *wkUController = [[WKUserContentController alloc] init];
        [wkUController addUserScript:wkUScript];
        
        WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init];
        wkWebConfig.userContentController = wkUController;
        _webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - 80, SCREEN_HEIGHT - kStatusBarHeight - 44 -70 -150) configuration:wkWebConfig];
        _webView.UIDelegate = self;
        _webView.navigationDelegate = self;
        _webView.scrollView.delegate = self;
            
        NSString *encodedString = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault,(CFStringRef)self._urlString,(CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]",NULL,kCFStringEncodingUTF8));
        [_webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:encodedString]]];
//            [self createXiYiView];
            
        [self.contentView addSubview:_webView];
        
    }
    return _webView;
}
#pragma mark - 代收协议相关
- (void)createXiYiView {
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT - kStatusBarHeight - 44 - 65, SCREEN_WIDTH, 65)];
    view.backgroundColor = [UIColor whiteColor];
    [self addSubview:view];
    
    CGFloat width = (SCREEN_WIDTH-40-40)/2;
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton setBackgroundColor:[UIColor whiteColor]];
    cancelButton.layer.masksToBounds = YES;
    cancelButton.layer.cornerRadius = 5;
    cancelButton.layer.borderWidth = 1;
    cancelButton.layer.borderColor = [UIColor colorWithRed:45/255.0 green:125/255.0 blue:246/255.0 alpha:1].CGColor;
    cancelButton.tag = 300;
    [cancelButton addTarget:self action:@selector(xiYiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [cancelButton setTitleColor:[UIColor cjMainColor] forState:UIControlStateNormal];
    cancelButton.frame = CGRectMake(20, 10, width, 45);
    [view addSubview:cancelButton];
    
    UIButton *confirmButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmButton setTitle:@"同意" forState:UIControlStateNormal];
    [confirmButton setBackgroundColor:[UIColor cjMainColor]];
    confirmButton.layer.masksToBounds = YES;
    confirmButton.layer.cornerRadius = 5;
    confirmButton.tag = 301;
    [confirmButton addTarget:self action:@selector(xiYiBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    confirmButton.frame = CGRectMake(60 + width, 10, width, 45);
    [view addSubview:confirmButton];
}

- (void)xiYiBtnClick:(UIButton *)button {
//    if (self.XieYiBlock) {
//        self.XieYiBlock(button.tag == 300 ? 0 : 1);
//        [self.navigationController popViewControllerAnimated:YES];
//    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if(self.isUrlloaded){
        if(scrollView.contentOffset.y >= (scrollView.contentSize.height - scrollView.frame.size.height)){
            self.comitButton.userInteractionEnabled = YES;
            self.comitButton.backgroundColor = [UIColor cjMainColor];
        }else if(scrollView.contentOffset.y < scrollView.contentSize.height){
            
        }
    }
}

@end
